package com.changan.exception;

import com.changan.response.Result;
import com.changan.response.ResultCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @program: xg-parent
 * @description: 全局异常处理类
 * @author: Mr.Nie
 * @create: 2022-03-02 11:05
 **/
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {


    /**
     * 功能描述: <br>
     * 〈算数异常〉
     * @Param: [e]
     * @Return: com.changan.response.Result
     * @Author: Mr.Nie
     * @Date: 2022/3/2 11:08
     */
    @ExceptionHandler(ArithmeticException.class)
    public Result arithmeticException(ArithmeticException e){
        log.info("算数异常:" + e.getMessage());
        e.printStackTrace();
        return Result.error(ResultCode.ARITHMETIC_EXCEPTION);
    }

    /**
     * 功能描述: <br>
     * 〈空指针异常〉
     * @Param: [e]
     * @Return: com.changan.response.Result
     * @Author: Mr.Nie
     * @Date: 2022/3/2 11:12
     */
    @ExceptionHandler(NullPointerException.class)
    public Result nullPointerException(NullPointerException e){
        e.printStackTrace();
        return Result.error(ResultCode.NULL_POINTER_EXCEPTION);
    }

    /**
     * 功能描述: <br>
     * 〈数组越界的异常〉
     * @Param: [e]
     * @Return: com.changan.response.Result
     * @Author: Mr.Nie
     * @Date: 2022/3/2 11:13
     */
    @ExceptionHandler(ArrayIndexOutOfBoundsException.class)
    public Result arrayIndexOutOfBoundsException(ArrayIndexOutOfBoundsException e){
        e.printStackTrace();
        return Result.error(ResultCode.ARRAY_INDEX_OUT_OF_BOUNDS_EXCEPTION);
    }

    /**
     * 功能描述: <br>
     * 〈除了上述之外其他的异常〉
     * @Param: [e]
     * @Return: com.changan.response.Result
     * @Author: Mr.Nie
     * @Date: 2022/3/2 11:13
     */
    @ExceptionHandler(Exception.class)
    public Result arrayIndexOutOfBoundsException(Exception e){
        e.printStackTrace();
        return Result.error(ResultCode.UNKNOWN_EXCEPTION);
    }

    /**
     * 功能描述: <br>
     * 〈处理业务异常〉
     * @Param: [e]
     * @Return: com.changan.response.Result
     * @Author: Mr.Nie
     * @Date: 2022/3/2 11:26
     */
    @ExceptionHandler(BusinessException.class)
    public Result arrayIndexOutOfBoundsException(BusinessException e){
        e.printStackTrace();
        return Result.error()
                .code(e.getCode())
                .message(e.getErrMsg());
    }
}
